package com.sky.utils;


import com.sky.constant.MessageConstant;
import com.sky.constant.RedisDataBaseNameConstant;
import com.sky.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class RedisDeleteKeysUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 删除指定的key(菜品分类)
     * @param DatabaseId
     */
    public void deleteDishKey(Long DatabaseId){
        try{
            log.info("删除Redis数据库{}",RedisDataBaseNameConstant.DISH_KEY+DatabaseId);
            redisTemplate.delete(RedisDataBaseNameConstant.DISH_KEY+DatabaseId);
        }catch (Exception e){
            log.error("删除Redis数据库{}失败",RedisDataBaseNameConstant.DISH_KEY+DatabaseId);
            throw new BaseException(MessageConstant.FAIL_TO_DELETE);
        }
        log.info("删除Redis数据库{}成功",RedisDataBaseNameConstant.DISH_KEY+DatabaseId);
    }
    /**
     * 删除多个key(菜品分类)
     * @param DatabaseIds
     */
    public void deleteDishKeys(List<Long> DatabaseIds){
      try{
          log.info("批量删除Redis数据库{}",RedisDataBaseNameConstant.DISH_KEY+DatabaseIds);
          for (Long DatabaseId : DatabaseIds) {
              deleteDishKey(DatabaseId);
          }
      }catch (Exception e){
          log.error("批量删除Redis数据库{}失败",RedisDataBaseNameConstant.DISH_KEY+DatabaseIds);
          throw new BaseException(MessageConstant.FAIL_TO_DELETE);
      }
      log.info("批量删除Redis数据库{}成功",RedisDataBaseNameConstant.DISH_KEY+DatabaseIds);
    }

    /**
     * 删除指定的key(套餐分类)
     * @param DatabaseId
     */
    public void deleteSetMealKey(Long DatabaseId){
        try{
            log.info("删除Redis数据库{}",RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseId);
            redisTemplate.delete(RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseId);
        }catch (Exception e){
            log.error("删除Redis数据库{}失败",RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseId);
            throw new BaseException(MessageConstant.FAIL_TO_DELETE);
        }
        log.info("删除Redis数据库{}成功",RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseId);
    }
    /**
     * 删除多个key(套餐分类)
     * @param DatabaseIds
     */
    public void deleteSetMealKeys(List<Long> DatabaseIds){
        try{
            log.info("批量删除Redis数据库{}",RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseIds);
            for (Long DatabaseId : DatabaseIds) {
                deleteSetMealKey(DatabaseId);
            }
        }catch (Exception e){
            log.error("批量删除Redis数据库{}失败",RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseIds);
            throw new BaseException(MessageConstant.FAIL_TO_DELETE);
        }
        log.info("批量删除Redis数据库{}成功",RedisDataBaseNameConstant.SET_MEAL_KEY_REDIS +DatabaseIds);
    }
}
